﻿using System;
using System.Collections.Generic;
using System.Collections;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_CompanyInfo_General : System.Web.UI.Page
{
    public ArrayList companyarr = null;
    public int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null) { Response.Redirect("../Index.aspx"); }

        companyarr = (ArrayList)DB.getArrCompania("");
        if (!Page.IsPostBack)
        {
            

            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());

            lb_companyid.Text = id.ToString();

            RE_GenericBean company = DB.getCompania(id);
            lb_companyid.Text = company.intC1.ToString();
            tb_companyname.Text = company.strC1;
            tb_taxid.Text = company.strC2;
            tb_address1.Text = company.strC3;
            tb_address2.Text = company.strC4;
            tb_city.Text = company.strC5;
            tb_state.Text = company.strC6;
            tb_zip.Text = company.strC7;
            tb_country.Text = company.strC8;
            tb_phone1.Text = company.strC9;
            tb_phone2.Text = company.strC10;
            tb_phone3.Text = company.strC11;
            tb_fax.Text = company.strC12;
            tb_website.Text = company.strC13;
            tb_contactemail.Text = company.strC14;

            tb_noaccount.Text = DB.getCountAccount(id).ToString();
            tb_numberemployees.Text = DB.getCountEmployee(id).ToString();

            if (company.intC1 != 0)
            {
                habilitocampos(false);
                HyperLink1_ModalPopupExtender.Show();
            }
        }
        else { 
            id=int.Parse(lb_companyid.Text.ToString());
        }

    }
    
    private void habilitocampos(bool estado){
        tb_companyname.Enabled = estado;
        tb_address1.Enabled = estado;
        tb_address2.Enabled = estado;
        tb_city.Enabled = estado;
        //tb_companyname.Enabled = estado;
        tb_contactemail.Enabled = estado;
        tb_country.Enabled = estado;
        tb_fax.Enabled = estado;
        //tb_noaccount.Enabled = estado;
        //tb_numberemployees.Enabled = estado;
        tb_phone1.Enabled = estado;
        tb_phone2.Enabled = estado;
        tb_phone3.Enabled = estado;
        tb_state.Enabled = estado;
        tb_taxid.Enabled = estado;
        tb_website.Enabled = estado;
        tb_zip.Enabled = estado;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        RE_GenericBean company = new RE_GenericBean();
        bool edit = false;

        company.intC1 = int.Parse(lb_companyid.Text);
        company.strC1 = tb_companyname.Text.ToUpper().Trim();
        company.strC2 = tb_taxid.Text.ToUpper().Trim();
        company.strC3 = tb_address1.Text.ToUpper().Trim();
        company.strC4 = tb_address2.Text.ToUpper().Trim();
        company.strC5 = tb_city.Text.ToUpper().Trim();
        company.strC6 = tb_state.Text.ToUpper().Trim();
        company.strC7 = tb_zip.Text.ToUpper().Trim();
        company.strC8 = tb_country.Text.ToUpper().Trim();
        company.strC9 = tb_phone1.Text.Trim();
        company.strC10 = tb_phone2.Text.Trim();
        company.strC11 = tb_phone3.Text.Trim();
        company.strC12 = tb_fax.Text.Trim();
        company.strC13 = tb_website.Text.Trim();
        company.strC14 = tb_contactemail.Text.Trim();

        if (company.intC1 != 0) edit = true;
        if (DB.InsertaCompanyInfoGeneral(company, edit)) Response.Redirect("CompanyInfo_General.aspx");
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        habilitocampos(true);
        HyperLink1_ModalPopupExtender.Show();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        id = int.Parse(lb_companyid.Text);
        if (id != 0)
        {
            int result = DB.DeleteConpania(id);
            Response.Redirect("CompanyInfo_General.aspx");
        }
    }
}